#!/bin/bash
datadir=/dev/shm
start=30001
num=4
delay=10
pip=223.95.196.68

cpunum=$( cat /proc/cpuinfo |grep "processor"|wc -l)
cpulimit=3

echo cpunum $cpunum

sleep 5

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
cp uam /opt/uam/
rm -rf $datadir/uam-data/

service docker start


for a in $( seq $start $((start+num-1)) )
do
if  (docker ps -a --format "table {{.Names}}" | grep uam$a)
then
   docker start uam$a
else
    echo run uam$a
    #mkdir -p $datadir/uam-data/uam$a/
    docker run --privileged --restart=unless-stopped -d -p $a:$a -p $((a+10000)):20000 \
    --cpus $cpulimit \
    --memory=3200M \
    -v $datadir/uam-data/uam$a:/root/.uam \
    -v /opt/uam:/opt/uam \
    -e PUBLIC_IP=$pip -e PORT=$a -e PUBLIC_KEY=1B171AA3285F8976A278A9C39DF418F19DFB43FE2793D29CD6A2630ADD65E651 \
    --name uam$a uam-docker
    sleep 10
    docker stop uam$a
    rm -rf $datadir/uam-data/uam$a/*.db
    rm -rf $datadir/uam-data/uam$a/*.db-shm
    rm -rf $datadir/uam-data/uam$a/*.db-wal
    cp n.db  $datadir/uam-data/uam$a/n.db
    cp c.db  $datadir/uam-data/uam$a/c.db
    docker start uam$a
fi
echo wait $((delay+($a-$start)/10))
sleep $((delay+($a-$start)/10))

done
